package com.htjc.integral.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author :zw
 * @className :JsonUtil
 */
public class JsonUtil {
	static Logger logger = LoggerFactory
			.getLogger(JsonUtil.class);

	private static final ObjectMapper mapper = new ObjectMapper();

	private JsonUtil() {

	}

	/**
	 * @param json 字符串
	 * @param cls  需要转成的bean类
	 * @return 转成的对象
	 */
	public static <T> T json2bean(String json, Class<T> cls) {
		try {
			return mapper.readValue(json, cls);
		} catch (Exception e) {
			logger.info("json", e.toString());
			return null;
		}
	}

	/**
	 * json 字符串转换为List
	 *
	 * @param json
	 * @param cls
	 * @return
	 */
	public static ArrayList<?> json2List(String json, Class<?> cls) {
		try {
			JavaType valueType = mapper.getTypeFactory().constructParametricType(ArrayList.class, cls);
			return (ArrayList<?>) mapper.readValue(json, valueType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 *  对象转化json字符串
	 *
	 * @param obj 需要转化的对象
	 * @return json字符串
	 */
	public static String bean2json(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * map转json
	 *
	 * @param obj 需要转化的对象
	 * @return json字符串
	 */
	public static String map2json(Map<String, Object> map) {
		try {
			return mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 *  判断某个Json字符串是否为一个标准的Json字符串
	 * @param jsonString
	 * @return
	 */
	public static Boolean isJsonObject(String jsonString){
		try{
			JSONObject.parse(jsonString);
			return true;
		}catch (Exception e){
			return false;
		}
	}
	/**
	 *  判断某个Json字符串是否为一个Json数组
	 * @param jsonObject
	 * @return
	 */
	public static Boolean isArrayOrObject(String jsonObject){
		try{
			JSONArray.parse(jsonObject);
			return true;
		}catch (Exception e){
			return false;
		}
	}


	/**
	 *   递归读取所有的key,验证接收参数
	 * @param jsonObject
	 */
	public static String getAllKey(JSONObject jsonObject,List<String> list) {
		long aa=System.currentTimeMillis();
		String str=null;
		Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
		while (keys.hasNext()) {
			String key = keys.next();
			list.add(key.toString());
			str=key.toString();
			if (jsonObject.get(key) instanceof JSONObject) {
				JSONObject innerObject = (JSONObject) jsonObject.get(key);
				list.add(getAllKey(innerObject, list));

			} else if (jsonObject.get(key) instanceof JSONArray) {
				JSONArray innerObject = (JSONArray) jsonObject.get(key);
				list.add(getAllKey(innerObject,list));
			}
		}
		return str;
	}
	public static String getAllKey(JSONArray json1,List<String> list) {
		//		StringBuffer stringBuffer = new StringBuffer();
		String str=null;
		if (json1 != null ) {
			Iterator<Object> i1 = json1.iterator();
			while (i1.hasNext()) {
				Object key = i1.next();
				if (key instanceof  JSONObject) {
					JSONObject innerObject = (JSONObject) key;
					//					stringBuffer.append(getAllKey(innerObject));
					str=getAllKey(innerObject,list);
				} else if (key instanceof JSONArray) {
					JSONArray innerObject = (JSONArray) key;
					//					stringBuffer.append(getAllKey(innerObject));
					str=getAllKey(innerObject,list);
				}else{
				}
			}
		}
		return str;
	}
	/**
	 * 引用传递判断查询出的key是否匹配
	 * @param arr
	 * @param value
	 * @param i
	 * @return
	 */
	public static  boolean useLoop(String[] arr,String value,StringBuffer i){
		int a=0;
		for(String s:arr){
			a++;
			if(s.equals(value)) {
				System.out.println(a);
				return true;
			}
		}
		i.append(a);
		return false;

	}

	public static void main(String[] args) {
//		List<String> aaa= new ArrayList();
//		getAllKey(JSONObject.parseObject("{'LOGIN_ACCOUNT_ID':'11'}"),aaa);
//		String[] bStrings=new String[aaa.size()];
//		String[] aStrings=IntroductionCheck.QUERY_INTEGRAL.getParams().split(",");
//		System.out.println(	Arrays.toString(aStrings));
//		bStrings=aaa.toArray(bStrings);
//		System.out.println(ArrayUtil.containsAll(bStrings, aStrings));
	}

}

